package com.AnywheeBt;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;


import android.util.Log;

public class PbCallList {
	protected static final String TAG = "PbCallList";
	public final static String BT_CALL_LIST="/data/data/com.AnywheeBt/bt_call_list";
	public final static String BT_PB_LIST ="/data/data/com.AnywheeBt/bt_pb_list";		
	public final static String BT_PB_TEMP ="/data/data/com.AnywheeBt/bt_pb_temp";

	public static int isSync = 0;
	public static boolean isDelete = false;
	
	private FileOutputStream mBtfdOut = null;
	private FileInputStream mBtfdIn = null;	
	private ArrayList<ItemList> BTList = new ArrayList<ItemList>();
	private RomCustomContactBean TelBook;
	private ArrayList<RomCustomContactBean>  BookList=new ArrayList<RomCustomContactBean>();
	public final static int MAX_CALL_LIST =100;
	public final static int MAX_PB_LIST =1000;
	public int nMaxList =MAX_PB_LIST;
	
	public class ItemList {
		String     strName;		
		String     strNum;
		String	   strTime; 
		String     strInOut;      
	}  
	
	public void DeleteListFile(String path) {
		File file = new File(path);
		if( file.exists() )  {
			file.delete();		
		}	
	}
	
	public void  ChangePbList() {
		byte[] buff=null;
		File file=null;
		int len=0;
		try {
			file = new File(BT_PB_TEMP);
			if( false==file.exists() ) return;
			len=(int)file.length();
			if( len>0 ) {
				buff=new byte[len+2];
				mBtfdIn = new FileInputStream(file);						    			
				mBtfdIn.read(buff);
				mBtfdIn.close();				
			} 				
		} catch (IOException e) {			
			e.printStackTrace();
			return;
		}			
		BTList.clear();
		int i=0;
		int pos=0;
		int num_len = 0;
		int name_len =0;
		int nocharcounts = 0;
		int thizN = 0;
		while(i< len ) {
			nocharcounts = 0;
			if( 0x0D==(buff[i] & 0xff) && 0x0A== (buff[i+1] & 0xff) ) {
				int size=i-pos;			//size stand for line lengths									
				thizN = 0;
				while(thizN < size)
				{
					if((0x39 >= (buff[pos + thizN] & 0xff)) && (0x30 <= (buff[pos + thizN] & 0xff)) && (thizN < 5)  )
					{
						nocharcounts ++;
					}
					
					if(0xFF == (buff[pos + thizN] & 0xFF))
					{
						name_len = thizN;
						num_len = size - 1 -name_len;
						break;
					}
					thizN++;
				}
				
				Log.e(TAG,"nocharcounts  ==== " + nocharcounts);
				int j=0;				
				String strName="";
				String strNum="";
				//String strTime="";
				{
					byte[] temp=new byte[name_len - nocharcounts];
					for(j=0;j<name_len - nocharcounts;j++) {
						temp[j]=buff[pos+ nocharcounts +j];
					}	
					try {						
						strName = new String(temp,"UTF-8");					
					} catch (UnsupportedEncodingException e) {						
						e.printStackTrace();
					}
				}
				{
					byte[] temp=new byte[num_len];
					for(j=0;j<num_len;j++) {
						temp[j]=buff[pos+name_len + 1 + j];
					}	
					try {						
						strNum = new String(temp,"UTF-8");					
					} catch (UnsupportedEncodingException e) {						
						e.printStackTrace();
					}
				}	
				//Log.e(TAG,"strName:"+strName+"  strNum:"+strNum);
				if( strName!="" && strNum!="" ) {
					ItemList item=new ItemList();					
					item.strName=strName;
					item.strNum=strNum;
					item.strTime=" ";
					item.strInOut="false";
					BTList.add(item);						
				}
				pos=i+2;
				if(pos >=len ) break;				
			}	
			i++;
		}
		Save_List(BT_PB_LIST);
	}


	public void Save_List(String path) {		
		try {
			int lenght=BTList.size();
			File file = new File(path);
			if( file.exists() ) {
				file.delete();
			}
			if( lenght>0 ) {				
				int i=0;
				mBtfdOut = new FileOutputStream(file);
				for(i=0;i<lenght;i++ )  {
					ItemList item=BTList.get(i);					
					String str=item.strName+"__"+item.strNum+"__"+item.strTime+"__"+item.strInOut+"\r\n";
					mBtfdOut.write(str.getBytes(),0,str.getBytes().length);									
				}
				mBtfdOut.close();
			}	
		} catch (IOException e) {			
			e.printStackTrace();  
		}				
	}
	
	public void get_list(String path) {
		BTList.clear();
		try {
			File file = new File(path);
			if(false==file.exists() ) return;
			int len=(int)file.length();
			if( len>0 ) {
				byte[] buff=new byte[len];
				mBtfdIn = new FileInputStream(file);						    			
				mBtfdIn.read(buff);
				mBtfdIn.close();				
				GetList(buff,len);
				mBtfdIn.close();
			} 
		} catch (IOException e) {			
			e.printStackTrace();  
		}	
	}	

	private void GetList(byte[] buff,int len) {						
		int pos=0;
		int i=0;
		while( i < (len-1) ) {		
			if( 0x0D==buff[i] && 0x0A==buff[i+1] ) {
				int size=i-pos;				
				byte[] b=new byte[size+1];
				b[size-1]=0;
				int j=0;
				for(j=0;j<size;j++ ) {
					b[j]=buff[pos+j];						
				}
				pos=i+2;
				if(pos >len ) break;
				String str = new String(b);			
				ItemList item=new ItemList();					
				item.strName=str.substring(0,str.indexOf("__"));
				str=str.substring(str.indexOf("__")+2,str.length());
				item.strNum=str.substring(0,str.indexOf("__"));		
				str=str.substring(str.indexOf("__")+2,str.length());
				item.strTime=str.substring(0,str.indexOf("__"));
				item.strInOut=str.substring(str.indexOf("__")+2,str.length());				
				BTList.add(item);
			}
			i++;
		}	
	}		
	
	public void InsertList(int index,ItemList item) {	
		int size =BTList.size();
		if( size<=0 )  BTList.add(item);
		else {
			int pos=0;
			if(index>0)  pos=index;
			if( size <index ) BTList.set(pos, item); 
			else BTList.add(item);
		}			
	}
		
	public void RemoveList(int index) {
		int size =BTList.size();
		if( size>0 ) {
			int pos=0;
			if(index>0)  pos=index;
			if( index>=size) BTList.remove(size-1);
			else BTList.remove(pos);
		}
	}

	public void Save_Max(int max) {
		nMaxList = max;
		while( BTList.size()>nMaxList ) {
			BTList.remove(BTList.size()-1);
		}
	}		
	
	public String GetListName(int pos) {
		int size=BTList.size();		
		String str="";
		if( pos<size ) {
			ItemList item=BTList.get(pos);
			str=item.strName;
		}
		return str;
	}	
	public String GetListNum(int pos) {
		int size=BTList.size();		
		String str="";
		if( pos<size ) {
			ItemList item=BTList.get(pos);
			str=item.strNum;
		}
		return str;
	}
	public String GetListTime(int pos) {
		int size=BTList.size();		
		String str="";
		if( pos<size ) {
			ItemList item=BTList.get(pos);
			str=item.strTime;
		}
		return str;
	}	
	public String GetListInOut(int pos) {
		int size=BTList.size();		
		String str="";
		if( pos<size ) {
			ItemList item=BTList.get(pos);
			str=item.strInOut;
		}
		return str;
	}
	
	public int  GetListSize() {
		return BTList.size();
	}
	public ArrayList<RomCustomContactBean> GetList(){
		String strName="";
		String phones="";
		for(int i=0;i<BTList.size();i++){
			TelBook=new RomCustomContactBean();
			ItemList item=BTList.get(i);
			strName=item.strName;
			phones=item.strNum;
			TelBook.setId(i);
			TelBook.setName(strName);
			TelBook.setPhones(phones);
			BookList.add(TelBook);
		}
		return BookList;
		
	}
	public void SetList(String name,String num,String time,String inout,int pos) {		
		ItemList item=new ItemList();		
		item.strName=name;
		item.strNum=num;
		item.strTime=time;
		item.strInOut=inout;
		InsertList(pos,item);
	}	
	
	public int FindListOfName(String name) {		
		int size=BTList.size(); 
		for(int i=0;i<size;i++ ) {
			ItemList item=BTList.get(i);
			String strName=item.strName;
			if( strName.equals(name) ) {
				return i;
			}
		}
		return -1;
	}		

	public int FindListofNum(String num) {		
		int size=BTList.size(); 
		for(int i=0;i<size;i++ ) {
			ItemList item=BTList.get(i);
			String strNum=item.strNum;
			if( strNum.equals(num) ) {
				return i;
			}
		}
		return -1;
	}		
	
}
